//This file is a part of StoragePlug for Minecraft.
//http://code.google.com/p/minecraft-storageplug/
//StoragePlug is Copyright (C) 2011 Christopher Smith
//Released under the MIT license:
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights 
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//DEALINGS IN THE SOFTWARE.
//
//Related to Minecraft Copyright (C) 2009-2011 Mojang http://www.minecraft.net/

package net.minecraft.src;

import java.io.File;
import java.util.List;
import java.util.logging.Logger;

import net.minecraft.storageplug.DataTree;
import net.minecraft.storageplug.Key;
import net.minecraft.storageplug.PluginLoader;
import net.minecraft.storageplug.ShutdownDisposeThread;
import net.minecraft.storageplug.SpChunkLoader;
import net.minecraft.storageplug.SpPlayerFileData;
import net.minecraft.storageplug.StorageProvider;

public class SaveOldDir
	implements ISaveHandler
{
	public static final Logger logger = Logger.getLogger("Minecraft");
	
	final File basedir;
	final File datadir;
	final String dirname;
	final boolean create;
	
	public final StorageProvider provider;

    public SaveOldDir(File file, String s, boolean flag)
    {    	
    	this.basedir = file;
    	this.datadir = new File(file, s);
    	this.dirname = s;
    	this.create = flag;
    	
    	try
		{
    		PluginLoader pl = PluginLoader.getPluginLoader();
    		
    		this.provider = pl.GetStorageInterface(datadir);
    		
            //Minecraft doesn't specify when it's done with the storage backend-- it just exits
            // some providers need to be disposed properly so that e.g. write caches can be flushed
            // so tie the dispose to VM exit time
            Thread cleanupThread = new Thread(new ShutdownDisposeThread(provider));
            Runtime.getRuntime().addShutdownHook(cleanupThread);
            
		} catch(Exception e)
		{
			throw new RuntimeException(e);
		}
    }
    
    public IChunkLoader getChunkLoader(WorldProvider worldprovider)
    {
        if(worldprovider instanceof WorldProviderHell)
        	return new SpChunkLoader(this, -1);
        else if(worldprovider instanceof WorldProviderEnd)
        	return new SpChunkLoader(this, 1);
        else
        	return new SpChunkLoader(this, 0);
    }

    public void saveWorldInfoAndPlayer(WorldInfo worldinfo, List players)
    {
        worldinfo.setSaveVersion(19132);
        try
		{
        	//store with player data?
        	NBTTagCompound nbt = worldinfo.storeLevelDataToNBT(players);
        	
//        	if(nbt.hasKey("Player"))
//        	{
//    			provider.writePlayer(nbt.getCompoundTag("Player"), new PlayerKey(" worldinfo player"));
//        	}
        	
			provider.writeDataTree(new DataTree(nbt), new Key.WorldInfo());
			
		} catch(Exception e)
		{
			//the original semantics were to do this:
			e.printStackTrace();
		}
    }
    
    public void saveWorldInfo(WorldInfo worldinfo)
    {
        worldinfo.setSaveVersion(19132);
        try
		{
        	provider.writeDataTree(new DataTree (worldinfo.getNBTTagCompound()), new Key.WorldInfo());
			
		} catch(Exception e)
		{
			//the original semantics were to do this:
			e.printStackTrace();
		}
    }
	
	@Override
	public WorldInfo loadWorldInfo()
	{
		try
		{
            DataTree dt = provider.readDataTree(new Key.WorldInfo());
			
			if(dt == null)
				return null;

			return new WorldInfo(dt.asNbtCompound());
			
		} catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public IPlayerFileData getPlayerNBTManager()
	{
		return new SpPlayerFileData(this);
	}

	@Deprecated
	@Override
	public File getMapFileFromName(String s)
	{
		return null;
	}
	
    @Deprecated
    public void func_22093_e()
    {}

	@Deprecated
	@Override
	public void checkSessionLock()
	{}
}
